﻿using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System;

namespace MapFormsApp.Models
{
    /// <summary>
    /// 校园建筑实体类（实现属性通知接口）
    /// </summary>
    public class Building : INotifyPropertyChanged
    {
        #region 属性定义
        private string _bldgId;
        private string _bldgName;
        private IPolygon _geometry;
        private int _floorCount;
        private bool _isSelected;

        /// <summary>
        /// 建筑唯一标识（如"A001"）
        /// </summary>
        public string BldgID
        {
            get => _bldgId;
            set => SetField(ref _bldgId, value);
        }

        /// <summary>
        /// 建筑名称（如"图书馆"）
        /// </summary>
        public string BldgName
        {
            get => _bldgName;
            set => SetField(ref _bldgName, value);
        }

        /// <summary>
        /// 建筑多边形几何（WGS84坐标系）
        /// </summary>
        public IPolygon Geometry
        {
            get => _geometry;
            set => SetField(ref _geometry, value);
        }

        /// <summary>
        /// 楼层数量
        /// </summary>
        public int FloorCount
        {
            get => _floorCount;
            set => SetField(ref _floorCount, value);
        }

        /// <summary>
        /// UI选择状态（用于地图高亮）
        /// </summary>
        public bool IsSelected
        {
            get => _isSelected;
            set => SetField(ref _isSelected, value);
        }
        #endregion

        #region 构造方法
        /// <summary>
        /// 从要素创建建筑对象
        /// </summary>
        public static Building FromFeature(IFeature feature)
        {
            if (feature == null) return null;

            return new Building
            {
                BldgID = feature.Value[feature.Fields.FindField("BldgID")]?.ToString(),
                BldgName = feature.Value[feature.Fields.FindField("BldgName")]?.ToString(),
                Geometry = feature.ShapeCopy as IPolygon,
                FloorCount = Convert.ToInt32(feature.Value[feature.Fields.FindField("FloorCount")])
            };
        }
        #endregion

        #region 几何操作
        /// <summary>
        /// 计算建筑占地面积（平方米）
        /// </summary>
        public double CalculateArea()
        {
            if (Geometry == null) return 0;

            IArea area = Geometry as IArea;
            return area?.Area ?? 0;
        }

        /// <summary>
        /// 检查点是否在建筑范围内
        /// </summary>
        public bool Contains(IPoint point)
        {
            if (Geometry == null || point == null) return false;

            IRelationalOperator relOp = Geometry as IRelationalOperator;
            return relOp?.Contains(point) ?? false;
        }

        /// <summary>
        /// 获取建筑中心点
        /// </summary>
        public IPoint GetCentroid()
        {
            if (Geometry == null) return null;

            IPoint centroid = new PointClass();
            ((IArea)Geometry).QueryCentroid(centroid);
            return centroid;
        }
        #endregion

        #region INotifyPropertyChanged 实现
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        #endregion

        #region COM对象管理
        /// <summary>
        /// 安全释放几何资源
        /// </summary>
        public void DisposeGeometry()
        {
            if (Geometry != null && Marshal.IsComObject(Geometry))
            {
                Marshal.ReleaseComObject(Geometry);
                Geometry = null;
            }
        }
        #endregion
    }
}